<template>
  <div class="base-info-view">
    <el-form
      class="form-detail"
      label-position="right"
      label-width="120px"
      style="padding-left:20px;"
    >
      <el-row
        :gutter="20"
        style="width:100%"
      >
        <el-col :span="12">
          <el-form-item label="配送商名称：">
            <div>{{ detail?.distributeName }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="配送产品信息：">
            <my-tooltip
              :content="getProductName(detail)"
              @click="toProductDetail"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="配送费：">
            <div>
              <div v-if="detail?.computeMode==='固定价格'">
                <span>{{ detail?.distributePrice }}元</span>
                <span v-if="detail?.sellUnit">/{{ getLabel('sell_unit',detail?.sellUnit) }}</span>
              </div>
              <div v-if="detail?.computeMode==='终端价百分比'">
                <span>{{ detail?.distributePrice }}%</span>
                <span>终端价</span>
              </div>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="配送服务编号：">
            <div>{{ detail?.distributeServeNo }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="配送地区：">
            <area-list-view :areas="detail?.region" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="关联配送合同：">
            <div>
              <el-link
                v-if="detail?.contractNumber "
                type="primary"
                :underline="false"
                @click="toContractDetail"
              >{{ detail?.contractNumber }}</el-link>
              <span v-else>无</span>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服务结束日期：">
            <div>{{ dateFormat('yyyy-mm-dd HH:MM', detail?.expireTime) }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="配送时效约定："
            style="width: 100%"
          >
            <div>{{ detail?.remark || '无' }}</div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- 产品详情 -->
    <detail-drawer
      ref="proDetailDrawer"
      custom-title
    >
      <template #title>
        <span>产品详情 {{ getProductName(detail) }}</span>
      </template>
      <template #contents>
        <div class="drawer-content">
          <div class="history-list">
            <item-title title="基本信息" />
            <product-base-info
              v-if="productId"
              :id="productId"
            />
          </div>
          <div class="history-list">
            <item-title title="修改记录" />
            <operation-view
              :id="productId"
              mode="product"
            />
          </div>
        </div>
      </template>
    </detail-drawer>
    <!--合同详情-->
    <detail-drawer
      ref="contractDrawer"
      custom-title
      :size-width="1000"
    >
      <template #title>
        <span>合同 {{ detail?.contractNumber }} </span>
      </template>
      <template
        v-if="contractId"
        #contents
      >
        <div class="drawer-content">
          <div class="history-list">
            <contract-detail
              ref="salesDetailDrawer"
              :contract-id="contractId"
            />
          </div>
        </div>
      </template>
    </detail-drawer>
  </div>
</template>

<script>
import { defineAsyncComponent, inject, reactive, ref, toRefs, watch } from 'vue'

export default {
  name: 'DistributorServiceInfo',
  components: {
    productBaseInfo: defineAsyncComponent(() =>
      import(
        '@CRM/views/commonView/detailComponents/product/productBaseInfo.vue'
      )
    ),
    contractDetail: defineAsyncComponent(() =>
      import('@CRM/views/commonView/contract/contractDetail.vue')
    ),
  },
  props: {
    id: {
      type: Number,
      default: 0,
    },
  },
  setup(props, context) {
    const api = inject('api')
    var proDetailDrawer = ref(null)
    var contractDrawer = ref(null)
    var reactiveData = reactive({
      detail: {},
      productId: 0,
      contractId: 0,
    })
    const toProductDetail = () => {
      reactiveData.productId = reactiveData.detail.productId
      proDetailDrawer.value.open()
    }
    const toContractDetail = () => {
      reactiveData.contractId = reactiveData.detail.contractId
      contractDrawer.value.open()
    }
    // 获取配送服务详情
    const getDistributeDetail = (id) => {
      api.distributors
        .DISTRIBUTORS_SERVICE_DETAILS({
          id: id,
        })
        .then((res) => {
          if (res.code) {
            console.log(res.msg)
          } else {
            reactiveData.detail = res.data.DistributeServe
          }
        })
    }
    watch(
      () => props.id,
      (newval, oldval) => {
        if (newval) {
          getDistributeDetail(newval)
        }
      },
      { immediate: true, deep: true }
    )
    return {
      ...toRefs(props),
      ...toRefs(reactiveData),
      proDetailDrawer,
      contractDrawer,
      toProductDetail,
      toContractDetail,
    }
  },
}
</script>

<style scoped>
</style>
